aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/notes/[id].js
diff options
context:
space:
mode:
Diffstat (limited to 'pages/api/notes/[id].js')
-rw-r--r--pages/api/notes/[id].js29
1 files changed, 17 insertions, 12 deletions
diff --git a/pages/api/notes/[id].js b/pages/api/notes/[id].js
index 12f0ba4..a2cd680 100644
--- a/pages/api/notes/[id].js
+++ b/pages/api/notes/[id].js
@@ -1,11 +1,11 @@
import dbConnect from 'configs/dbConnect'
import withSession from 'hocs/withSession'
-import NoteList from 'models/NoteList'
-import Note from 'models/Note'
+import NoteList from 'apps/Notes/models/NoteList'
+import Note from 'apps/Notes/models/Note'
export default withSession(async (req, res) => {
+ const conn = await dbConnect()
const { id: _id } = req.query
- await dbConnect()
switch (req.method) {
case 'GET':
@@ -24,12 +24,12 @@ export default withSession(async (req, res) => {
res.status(200).json(note)
} catch (error) {
- console.log(error)
res.status(400).json({ error: true })
}
break
case 'DELETE':
try {
+ const session = await conn.startSession()
const user = req.session.get('user')
if (!user || !user?.isVerified || !_id) {
@@ -39,17 +39,20 @@ export default withSession(async (req, res) => {
const noteId = await NoteList.getNoteId(user.noteList, _id)
if (!noteId) throw new Error('Something went wrong')
- await Note.findByIdAndDelete(noteId)
- const { notes } = await NoteList.removeNote(user.noteList, _id)
+ await session.withTransaction(async () => {
+ await Note.findByIdAndDelete(noteId)
+ const { notes } = await NoteList.removeNote(user.noteList, _id)
- res.status(200).json(notes)
+ session.endSession()
+ res.status(200).json(notes)
+ })
} catch (error) {
- console.log(error)
res.status(400).json([])
}
break
case 'PUT':
try {
+ const session = await conn.startSession()
const user = req.session.get('user')
const { title, noteId, content } = req.body
@@ -57,12 +60,14 @@ export default withSession(async (req, res) => {
throw new Error('Something went wrong')
}
- await Note.updateNote(noteId, content)
- const { notes } = await NoteList.updateList(user.noteList, noteId, title)
+ await session.withTransaction(async () => {
+ await Note.updateNote(noteId, content)
+ const { notes } = await NoteList.updateList(user.noteList, noteId, title)
- res.status(200).json(notes)
+ session.endSession()
+ res.status(200).json(notes)
+ })
} catch (error) {
- console.log(error)
res.status(400).json([])
}
break